<?php

namespace App\Handler\Admin\Product\Product;

if (!defined('IN_PX'))
    exit;

use App\Handler\Admin\AbstractCommon;
use Phoenix\Support\MsgHelper;
use App\Service\UPFile;
use Phoenix\Log\Log4p as logger;
use App\Tools\Html;

/**
 * 读取
 */
class UploadExcel extends AbstractCommon {

    protected function __Inject($db, $cache, $session, UPFile $upFile) {}


    public function processRequest(Array & $context) {
        $add_date = time(); //插入数据库的时间
        require_once(ROOT_PATH . 'vendor/PHPExcel/PHPExcel.php'); //引入读取excel的类文件
        $filename = ROOT_PATH . 'uploads/files/' . $_POST['filename'];
        $fileType = \PHPExcel_IOFactory::identify($filename);//自动获取文件的类型提供给phpexcel用
        $objReader = \PHPExcel_IOFactory::createReader($fileType);//获取文件读取操作对象
        $objPHPExcel = $objReader->load($filename); //加载文件

        $highestRow = $objPHPExcel->getSheet(0)->getHighestRow(); //取得总行数
        $highestColumn = $objPHPExcel->getSheet(0)->getHighestColumn(); //取得总列数
        //$highestColumn = 'Q'; //取得总列数

        try {
            $this->db->beginTransaction();
            $_identity = 0;
            //循环读取excel文件,读取一条,插入一条
            for ($j = 2; $j <= $highestRow; $j++) { //从第2行开始读取数据
                $str = '';
                if ($objPHPExcel->getActiveSheet()->getCell("B$j")->getValue() != "") { //openid不能为空
                    for ($k = 'A'; $k <= $highestColumn; $k++) {
                        //从A列读取数据
                        //这种方法简单，但有不妥，以'\\'合并为数组，再分割\\为字段值插入到数据库,实测在excel中，
                        //如果某单元格的值包含了\\导入的数据会为空
                        $_cell = $objPHPExcel->getActiveSheet()->getCell("$k$j"); //单元格
                        $_value = $_cell->getValue(); //读取单元格内容
                        //日期格式转换成时间缀格式
//                        if ($_cell->getDataType() == \PHPExcel_Cell_DataType::TYPE_NUMERIC) {
//                            $cellStyleFormat = $_cell->getStyle($_cell->getCoordinate())->getNumberFormat();
//                            $formatCode = $cellStyleFormat->getFormatCode();
//                            if (preg_match('/^(\[\$[A-Z]*-[0-9A-F]*\])*[hmsdy]/i', $formatCode)) {
//                                $_value = \PHPExcel_Shared_Date::ExcelToPHP($_value);
//                            }
//                        }
                        $str .= $_value . '\\';
                    }
                }
                $rows = explode("\\", $str);

               if (!is_null($rows)) {
                   logger::d($rows);
                   $post = array(
                       'category_id' => intval($rows[0]),
                       'title' => Html::getTextToHtml($rows[1]),
                       'synopsis' => Html::getTextToHtml($rows[2]),
                       'cover' => '',
                       'is_home_display' => 0,
                       'is_status' => 0,
                       'is_display' => $rows[3],
                       'add_date' => time(),
                       'release_date' => time(),
                       'language' => 0,
                       'sort' => intval($rows[4]),
                       'catalog' => Html::getTextToHtml($rows[5]),
                       'clone' => Html::getTextToHtml($rows[6]),
                       'swissport' => Html::getTextToHtml($rows[7]),
                       'KDA' => Html::getTextToHtml($rows[8]),
                       'mg' => Html::getTextToHtml($rows[9]),
                       'application' => Html::getTextToHtml($rows[10]),
                       'organization' => Html::getTextToHtml($rows[11]),
                       'Immunogen' => Html::getTextToHtml($rows[12]),
                       'specifications' => Html::getTextToHtml($rows[13]),
                       'left' => Html::getTextToHtml($rows[14]),
                       'right' => Html::getTextToHtml($rows[15]),
                   );
                   $this->db->debug();
                   $isture = $this->db->table('`#@__@archives`')
                       ->row(array(
                           '`category_id`' => '?',
                           '`title`' => '?',
                           '`synopsis`' => '?',
                           '`cover`' => '?',
                           '`is_home_display`' => '?',
                           '`is_status`' => '?',
                           '`is_display`' => '?',
                           '`add_date`' => '?',
                           '`release_date`' => '?',
                           '`language`' => '?',
                           '`sort`' => '?',
                           '`catalog`' => '?',
                           '`clone`' => '?',
                           '`swissport`' => '?',
                           '`KDA`' => '?',
                           '`mg`' => '?',
                           '`application`' => '?',
                           '`organization`' => '?',
                           '`Immunogen`' => '?',
                           '`specifications`' => '?',
                           '`left`' => '?',
                           '`right`' => '?',
                       ))
                       ->bind($post)
                       ->save();

                   $_POST['substance'] = '';

                   $this->db->table('`#@__@archives_substance`')
                       ->row(array(
                           '`archives_id`' => '?',
                           '`substance`' => '?'
                       ))
                       ->bind(array(
                           $isture,
                           $_POST['substance']
                           //, $_POST['substance']
                       ))
                       ->save();

                   if (!$isture) {
                       $this->db->rollBack();
                       echo(MsgHelper::json('DB_ERROR'));
                   }
               }
            }
            $this->db->commit();
            echo(MsgHelper::json('SUCCESS'));
        } catch (\Exception $e) {
            $this->db->rollBack();
            echo(MsgHelper::json('DB_ERROR'));
        }
    }
}
